<template>
<div class="goods-table">
  <el-table
    :data="pi.list"
    style="width: 100%">
    <el-table-column
      prop="id"
      label="商品编号"
      width="180">
    </el-table-column>
    <el-table-column
      prop="goodsName"
      label="商品名称"
      width="180">
    </el-table-column>
    <el-table-column
      prop="price"
      label="商品价格"
      width="180">
    </el-table-column>
    <el-table-column
      prop="description"
      label="商品信息"
      width="200">
    </el-table-column>
    <el-table-column
      prop="createTime"
      label="日期"
      width="180">
    </el-table-column>
    <el-table-column
      label="操作">
      <template slot-scope="scope">
        <router-link :to="{path:'/goods/update',query:{id:scope.row.id}}"><el-button size="mini">编辑</el-button></router-link>
        <el-button type="danger" size="mini" @click="removeGoods(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    layout="sizes, prev, pager, next, jumper, ->, total"
    :total="pi.total"
    :page-size.sync="pi.pageSize"
    :page-sizes="[2,5,10,50]"
    :current-page.sync="pi.pageNum"
    :pager-count="7"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    background>
  </el-pagination>
</div>
</template>
<script>
export default {
  name: "GoodsTable",
  data(){
    return{
      pi:{}
    }
  },
  created() {
    this.handleClick(1,2)
  },
  methods:{
    handleSizeChange(size){
      this.handleClick(this.pi.pageNum,size);
    },
    handleCurrentChange(page){
      this.handleClick(page,this.pi.pageSize);
    },
    handleClick(pageNum,pageSize){
      this.axios.get("/GoodsManager/goods",{
        params:{
          pageNum:pageNum,
          pageSize:pageSize
        }
      }).then(resp=>{
        this.pi = resp.data.data;
      }).catch(error=>console.log(error))
    },
    removeGoods(id){
      let isDelete =confirm("确认删除吗");
      if (!isDelete){
        return;
      }
      this.axios.delete("/GoodsManager/goods/"+id)
        .then(resp=>{
          if (resp.data.code==200&&resp.data.message=="success"){
            alert("删除成功");
            this.handleClick(this.pi.pageNum,this.pi.pageSize);
          }else {
            alert("删除失败");
          }
        }).catch(error=>console.log(error));
    }
  }
}
</script>

<style scoped>

</style>
